package com.sywb.chuangyebao.view.dialog;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.czt.mp3recorder.MP3Recorder;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.utils.ToastUtils;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class AskAudioDialog extends BaseDialogFragment {

    @BindView(R.id.iv_record)
    ImageView ivRecord;
    @BindView(R.id.tv_audio_time)
    TextView tvAudioTime;
    @BindView(R.id.iv_record_red)
    ImageView ivRecordRed;
    @BindView(R.id.iv_record_blue)
    ImageView ivRecordBlue;
    @BindView(R.id.rl_record)
    RelativeLayout rlRecord;
    @BindView(R.id.tv_record60)
    TextView tvRecord60;

    public static AskAudioDialog newInstance(Object... pramars) {
        AskAudioDialog dialogFragment = new AskAudioDialog();
        Bundle bundle = new Bundle();
        if (pramars != null && pramars.length > 0) {
            for (int i = 0; i < pramars.length; i++) {
                bundle.putSerializable("p" + i, (Serializable) pramars[i]);
            }
        }
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    private MP3Recorder mp3Recorder;

    private int recLen = 0;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @SuppressLint("SetTextI18n")
        @Override
        public void run() {
            if (handler == null&&tvAudioTime!=null) {
                return;
            }

            String tip = "0:";
            recLen++;
            if (recLen == 60) {
                ToastUtils.show(mActivity, R.string.audio60);
                afterRecord();
                return;
            }
            tvAudioTime.setText(tip + (recLen > 9 ? String.valueOf(recLen) : "0" + recLen));

            Logger.e("volume:" + mp3Recorder.getRealVolume());

            handler.postDelayed(this, 1000);
        }
    };

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.dialog_ask_audio;
    }

    @Override
    public void initView(View view, Bundle bundle) {
//        getDialog().setCancelable(false);// dialog弹出后会点击屏幕或物理返回键，dialog不消失
//        getDialog().setCanceledOnTouchOutside(false);// dialog弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失
//        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
//            @Override
//            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                if (keyCode == KeyEvent.KEYCODE_BACK) {
//                    return true;
//                }
//                return false;
//            }
//        });
    }

    @OnClick({R.id.iv_record_red, R.id.iv_record_blue})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_record_red:
                handler.postDelayed(runnable, 1000);
                tvAudioTime.setText("0:00");
                ivRecord.setVisibility(View.VISIBLE);
                tvRecord60.setVisibility(View.VISIBLE);
                ivRecordRed.setVisibility(View.GONE);
                ivRecordBlue.setVisibility(View.VISIBLE);

                if (mp3Recorder == null) {
                    mp3Recorder = new MP3Recorder();
                }
                try {
                    mp3Recorder.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.iv_record_blue:
                afterRecord();
                break;
        }
    }

    private void afterRecord() {
        if (mp3Recorder != null) {
            mp3Recorder.stop();
        }
if(handler!=null){
    handler.removeCallbacks(runnable);
    handler = null;
}

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("filePath", mp3Recorder.getFilePath());
        hashMap.put("fileLength", recLen);
        hashMap.put("audioLength", tvAudioTime.getText().toString());
        RxBus.get().post(Constants.AUDIOCOMPLETE, JSON.toJSONString(hashMap));


        dismiss();
    }

    @Override
    public boolean isMatchParent() {
        return true;
    }
}
